package com.cychat.mall.component;

import com.Cychat.commons.entity.pojo.Product;
import com.cychat.mall.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Auther: 刘俊  <br/>
 * Date: 2025-11-8:下午 3:37  <br/>
 * Description: 启动时同步数据库库存到Redis
 */
@Component
public class RedisInitRunner implements CommandLineRunner {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void run(String... args) throws Exception {
        List<Product> products = productMapper.selectList(null);
        for (Product p : products) {
            stringRedisTemplate.opsForValue().set("stock:" + p.getProductId(), String.valueOf(p.getStock()));
        }
    }
}
